<?php 
include_once("../../../wp-blog-header.php"); 

$formBody = "";
$arrayPages = "";

/**
 * Gets all the pages from a given parent
 *
 * @param int $parent Parent ID or 0 for no parent
 * @param array $exclude Pages to exclude
 * @return array Array of objects containing the page information
 */
function wpPageLinker_getPages($parent = 0, $exclude = ""){
	global $wpdb;
	//wp-group-restriction integration
	$excludeString = "";
	if($exclude != ""){
		foreach($exclude as $page){
			if(is_numeric($page))
				$excludeString .= "AND ID <> '$page' ";
		}
	}
	
	$query = "SELECT * FROM {$wpdb->posts} ";
	$query .= "WHERE post_status = 'publish' AND ";
	$query .= "post_type='page' AND post_parent='$parent' $excludeString ORDER BY post_title;";
	return $wpdb->get_results($query);
}

/**
 * Enter description here...
 *
 * @param int $parent Parent
 * @param int $level
 * @param array $exclude
 */
function wpPageLinker_printTree($parent, $level, $exclude){
	global $formBody, $arrayPages;
	$spacer = "";
	for($i = 0; $i < $level; $i++){
		$spacer .= "&nbsp;&nbsp;&nbsp;";
	}
	$formBody .=  "<div>{$spacer}";

	$formBody .=  "<input type='radio' name='page' id='page_{$parent->ID}' value='".$parent->ID."' />\n";
	$formBody .=  "<label for='page_{$parent->ID}'>{$parent->post_title}</label></div>\n";
	$arrayPages .= "pages[{$parent->ID}]=new Array('".get_page_link($parent->ID)."','$parent->post_title');\n";
	$filhos = wpPageLinker_getPages($parent->ID, $exclude);
	foreach ($filhos as $filho){
		wpPageLinker_printTree($filho, $level+1, $exclude);
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>{$lang_wpPageLinker_title}</title>
		<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
		
		<script language="javascript" type="text/javascript" src="../../../wp-includes/js/tinymce/tiny_mce_popup.js"></script>
		<script language="javascript" type="text/javascript" src="../../../wp-includes/js/tinymce/utils/mctabs.js"></script>
		<script language="javascript" type="text/javascript" src="../../../wp-includes/js/tinymce/utils/form_utils.js"></script>
		<script language="javascript" src="form.js" type="text/javascript" ></script>
</head>
	<body onload="tinyMCEPopup.executeOnLoad('init();');">
		<form onsubmit="updateAction();return false;">
			Select the page you wish to link to:
			<div class="panel_wrapper" style="overflow:auto; height:330px; border-top: 1px #919b9c solid;">
<?php
$pagesExclude = array();
if(class_exists("userGroups")){
	$groups = new userGroups();
	$pagesString = $groups->getPagesToExclude();
	$pagesExclude = explode(",",$pagesString);
}


$parents = wpPageLinker_getPages(0,$pagesExclude);

$level = 0;
$disabled = "";
if($parents == "" || count($parents) == 0){
	echo "No pages were found.";
	$disabled = "DISABLED style=\"color: lightgrey; background: #f0f0f0; border-left-color: #999; border-top-color: #999; border: 3px double #ccc;\"";
}else{
	foreach ($parents as $parent){
		wpPageLinker_printTree($parent, $level, $pagesExclude);
	}
}
echo "<script language=\"javascript\" type=\"text/javascript\">\n";
echo "var pages = new Array();\n";
echo $arrayPages;
echo "</script>\n";
echo $formBody;
?>
			</div>
			<div class="mceActionPanel">
				
	
				<div style="float: left">
					<input type="button" id="cancel" name="cancel" value="Cancel" onclick="tinyMCEPopup.close();" />
				</div>
				<div style="float: right">
					<input class="mcebutton" <?php echo $disabled ?> type="button" id="insert" name="insert" value="Add link" onclick="updateAction();" />
				</div>
			</div>
		</form>
	</body>
</html>